/*
 * @Author: your name
 * @Date: 2023-09-30 21:03:58
 * @LastEditTime: 2023-11-17 16:32:19
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \Angle\User\OperationSystem\system.c
 */
/**_Event
 * @file system_event.c
 * @author Cui Qiquan 
 * @brief 此文件用于向任务发送各种突发事件队列
 * @version 0.1
 * @date 2022-11-15
 * 
 * @copyright Copyright (c) 2022
 * 
 */

#include "system.h"
#include "sys_support_devices.h"
#include "system_support.h"

#define Tiny_Stack_Size       64
#define Small_Stack_Size      128
#define Normal_Stack_Size     256
#define Large_Stack_Size      512
#define Huge_Stack_Size       1024
#define PriorityVeryLow       1
#define PriorityLow           2
#define PriorityBelowNormal   3
#define PriorityNormal        4
#define PriorityAboveNormal   5
#define PriorityHigh          6
#define PrioritySuperHigh     7
#define PriorityRealtime      8

SystemStructdef System;

void System_Init(void)
{
	int8_t ret = 0;
	
	/* 创建消息队列 */
	System.SystemEvent.Flow_UartRxQueue = xQueueCreate(1, sizeof(EventStructdef));
   	System.SystemEvent.UAV_UartTxQueue  = xQueueCreate(3, sizeof(EventStructdef));
   	System.SystemEvent.UAV_UartRxQueue  = xQueueCreate(3, sizeof(EventStructdef));
   	System.SystemEvent.VL53L1_EXTIQueue  = xQueueCreate(1, sizeof(EventStructdef));
   	System.SystemEvent.VL53L1_TimerQueue = xQueueCreate(1, sizeof(EventStructdef));
   	System.SystemEvent.IMU_Sensor_EXTIQueue = xQueueCreate(1, sizeof(EventStructdef));
   	System.SystemEvent.Magnetometer_EXTIQueue = xQueueCreate(1, sizeof(EventStructdef));

   	System.UserQueues.UAV_SendDataQueue = xQueueCreate(10, sizeof(EventStructdef));
	System.UserQueues.ImuDataQueue = xQueueCreate(1, sizeof(sensor_imu_msg));
	System.UserQueues.MagDataQueue = xQueueCreate(1, sizeof(sensor_mag_msg));
	System.UserQueues.AngleDataQueue = xQueueCreate(1, sizeof(ahrs_angle_msg));

	/* 创建信号量 */
	System.UserSems.AttitudeCalSem = xSemaphoreCreateBinary();
	System.UserSems.SendDataSem = xSemaphoreCreateBinary();
	
	/* 创建消息队列集 */
	System.SensorEventsSet = xQueueCreateSet(4);	
	xQueueAddToSet(System.SystemEvent.Flow_UartRxQueue, System.SensorEventsSet);
	xQueueAddToSet(System.SystemEvent.VL53L1_TimerQueue, System.SensorEventsSet);
	xQueueAddToSet(System.SystemEvent.IMU_Sensor_EXTIQueue, System.SensorEventsSet);
	xQueueAddToSet(System.SystemEvent.Magnetometer_EXTIQueue, System.SensorEventsSet);

	/* 创建用户任务 */
	ret |= xTaskCreate(SensorTaskFunc,   "SensorTask",   Normal_Stack_Size, NULL, PriorityBelowNormal, System.UserTasks.SensorTask);
	ret |= xTaskCreate(SendDataTaskFunc, "SendDataTask", Normal_Stack_Size,  NULL, PriorityAboveNormal, System.UserTasks.SendDataTask);
	ret |= xTaskCreate(AttitudeCalFunc, "AttitudeCalTask", Large_Stack_Size,  NULL, PriorityNormal, System.UserTasks.AttitudeCalTask);

	if(ret != pdPASS)
		elog_error("", "task create fail");
}



